<?php defined('BASEPATH') or exit('No direct script access allowed');

class Account_m extends MY_Model {
	
	function __construct(){
		parent::__construct();
		$this->load->library('module_helper');
	}
	
	function index(){
		$USER = $this->user_entity->init('id',$_SESSION['user']['id']);
		return $USER;
	}
	
	function submit_information(){
		 
		$USER = $this->user_entity->init('id',$_SESSION['user']['id']);
		//get old data
		$revision = $USER->buildNewRevision();
		
		$company_profile = "";
		if(isset($_FILES["company_profile"]) AND !empty($_FILES["company_profile"]['name'])){
			if(($_FILES["company_profile"]['size']/1000000) > allowMaxFileSize() ){
				$company_profile = '';
			}else{
				$company_profile = $this->module_helper->uploadFile( 
										$_FILES["company_profile"]['tmp_name'],
										$_FILES["company_profile"]['name'],
										$USER->getFolderFileStorage(),
										allowExtensionFileDocumentUpload()
									);
			}	
			if(empty($company_profile)){
				echo "err#company_profile";exit();
			}
		}
		$array_init = array('show_type', 'country_origin', 'music_genre');
		foreach($array_init as $element){
			if(!isset($_POST[$element])){
				$_POST[$element] = array();
			}
		}
		$USER->setFirstName( $this->input->post('first_name','') );
		$USER->setLastName( $this->input->post('last_name','') );
		$USER->setJobTitle( $this->input->post('job_title','') );
		$USER->setCompany( $this->input->post('company','') );
		$USER->setCompanyUrl( $this->input->post('company_url','') );
		$USER->setCompanyProfile( $company_profile );
		$USER->setCountry( $this->input->post('country','') );
		$USER->setAddressLine1( $this->input->post('address_line_1','') );
		$USER->setAddressLine2( $this->input->post('address_line_2','') );
		$USER->setCity( $this->input->post('city','') );
		$USER->setState( $this->input->post('state','') );
		$USER->setZip( $this->input->post('zip','') );
		$USER->setWorkPhone( $this->input->post('work_phone','') );
		$USER->setMobile( $this->input->post('mobile','') );	
	//	$USER->setBankNameAcc( $this->input->post('bank_name_acc','') );
	//	$USER->setBankAccNo( $this->input->post('bank_acc_no','') );
	//	$USER->setBankName( $this->input->post('bank_name','') );
	//	$USER->setBankAddress( $this->input->post('bank_address','') );
	//	$USER->setSwiftCode( $this->input->post('swift_code','') );
		$USER->setShowType( implode(',',$_POST['show_type']) );
		$USER->setCountryOrigin( implode(',',$_POST['country_origin']) );
		$USER->setMusicGenre( implode(',',$_POST['music_genre']) );
			
		$USER->setRevision($revision);
		$USER->update();
		
		$this->crontab_email->cronUpdate( $USER->getAccCode(),'_sendEmailToUser_editAccNotify', $USER->getId(), array(time()) );	
		echo 'ok';
		exit();
	}
	
	function submit_password(){
		$USER = $this->user_entity->init('id',$_SESSION['user']['id']);
		$revision = $USER->buildNewRevision();
		
		$USER->setPassword( $this->input->post('new_password','') );
		$USER->setRevision($revision);
		
		$USER->update();
			
		echo 'ok';
		exit();
	}
	
	function submit_email(){
		$USER = $this->user_entity->init('id',$_SESSION['user']['id']);
		$newEmail = $this->input->post('new_email','');
		
		if($USER->getEmail() == $newEmail){
			echo 'ok';exit();
		}else{
			$OTHER_USER = $this->user_entity->init('email',$newEmail);
			if($OTHER_USER){
				echo 'err#This is email  already existed in system. You can not use this email.';exit();
			}else{
				$revision = $USER->buildNewRevision();
				
				$USER->setEmail($newEmail);
				$USER->setRevision($revision);
				
				$USER->update();
				
				$this->crontab_email->cronUpdate( $USER->getAccCode(),'_sendEmailToUser_editAccNotify', $USER->getId(), array(time()) );	
				
				echo 'ok';
				exit();
			}
		}
	} 
	
	
	
}